class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        len_s, len_p = len(s), len(p)
        result = []
        if len_p > len_s:
            return result
        p_count = {}
        window_count = {}
        # 初始化
        for i in range(len_p):
            p_count[p[i]] = p_count.get(p[i], 0) + 1
            window_count[s[i]] = window_count.get(s[i], 0) + 1
        if p_count == window_count:
            result.append(0)
        # 剩余部分
        for i in range(len_p, len_s):
            leftchar = s[i - len_p]
            window_count[leftchar] -= 1
            if window_count[leftchar] == 0:
                del window_count[leftchar]  # 如果不删除 'a' 这个，会被判定为不相等
            right_char = s[i]
            window_count[right_char] = window_count.get(right_char, 0) + 1
            if p_count == window_count:
                result.append(i - len_p + 1)
                
        return result
